import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STChange, STColumn, STComponent, STData } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DictService } from '@core';
import { SysTenantEditComponent } from './edit/edit.component';
import { environment } from '@env/environment';

@Component({
  selector: 'app-sys-tenant',
  templateUrl: './tenant.component.html',
})
export class SysTenantComponent implements OnInit {
  isGeneralQuery = true;
  selectedRows: STData[] = [];
  keyword: string = '';
  
  url = `${environment.sys.ctx}/sysTenant/findByQueryParams`;
  searchSchema: SFSchema = {
    properties: {
      name: {
        type: 'string',
        title: '名称',
      },
    },
  };

  @ViewChild('st', { static: false }) st!: STComponent;
  columns: STColumn[] = [
    { title: 'ID', index: 'id', type: 'checkbox' },
    { title: '代码', index: 'code' },
    { title: '名称', index: 'name' },
    { title: '描述', index: 'description' },
    {
      title: '是否有效',
      index: 'enabled',
      type: 'yn',
      yn: { truth: 1 },
    },
    {
      title: '操作',
      width: 100,
      buttons: [
        // { text: '查看', click: (item: any) => `/form/${item.id}` },
        { icon: 'edit', type: 'static',  modal: {component: SysTenantEditComponent }, click: 'reload', tooltip: `编辑` },
        {
          tooltip: `删除`,
          icon: 'delete',
          type: 'del',
          pop: {
            title: '是否确定删除?',
            okType: 'danger',
            icon: 'star',
          },
          click: (record, _modal, comp) => this.delete(record, _modal, comp),
        },
      ],
    },
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private dictService: DictService,
    private msgSrv: NzMessageService,
  ) {}

  ngOnInit() {}

  delete(record: any, _modal: any, comp: any) {
    this.http.post(`${environment.sys.ctx}/sysTenant/deleteById/${record.id}`).subscribe(res => {
      this.msgSrv.success(`成功删除`);
      this.st.reload();
    });
  }

  add() {
    this.modal.createStatic(SysTenantEditComponent, { i: {} }).subscribe(() => this.st.reload());
  }

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  
  deletes() {
    const ids = this.selectedRows.map((row)=>{return row.id})
    this.http.post(`${environment.sys.ctx}/sysTenant/deleteByIds`,ids).subscribe(res => {
      this.msgSrv.success(`成功删除`);
      this.st.reload();
    });
  }
}
